<template>
    <div class="system-aside" :class="{'is-collapse': collapse}">
        <div class="aside-menu">
            <el-menu
                :default-active="activePath"
                :collapse="collapse"
                background-color="transparent"
                active-text-color="#409EFF"
                unique-opened
                router
            >
                <template v-for="(firstLevel, index) in menuList">
                    <template v-if="firstLevel.children">
                        <el-submenu :index="firstLevel.path" :key="firstLevel.id">
                            <template slot="title">
                                <i :class="firstLevel.icon"></i>
                                <span slot="title">{{ firstLevel.text }}</span>
                            </template>
                            <template v-for="(secondLevel, index) in firstLevel.children">
                                <el-submenu v-if="secondLevel.children" :index="secondLevel.path" :key="secondLevel.id">
                                    <template  slot="title">
                                        <i :class="secondLevel.icon"></i>
                                        <span slot="title">{{ secondLevel.text }}</span>
                                    </template>
                                    <el-menu-item v-for="(thirdLevel, index) in secondLevel.children" :index="thirdLevel.path" :key="index">
                                        <i :class="thirdLevel.icon"></i>
                                        <span slot="title">{{ thirdLevel.text }}</span>
                                    </el-menu-item>
                                </el-submenu>
                                <el-menu-item v-else :index="secondLevel.path" :key="index">
                                    <i :class="secondLevel.icon"></i>
                                    <span slot="title">{{ secondLevel.text }}</span>
                                </el-menu-item>
                            </template>
                        </el-submenu>
                    </template>
                    <template v-else>
                        <el-menu-item :index="firstLevel.path" :key="index">
                            <i :class="firstLevel.icon"></i>
                            <span slot="title">{{ firstLevel.text }}</span>
                        </el-menu-item>
                    </template>
                </template>
            </el-menu>
        </div>

        <div class="aside-bottom">
            <div class="bottom-text">欢迎访问</div>
            <div id="collapse-btn" class="collapse-btn" @click="changeCollapse">
                <i v-if="collapse" class="el-icon-s-unfold"></i>
                <i v-else class="el-icon-s-fold"></i>
            </div>
        </div>
    </div>
</template>
<script>
    export default {
        name: 'system-menu',
        data () {
            return {
                menuList: []
            }
        },
        computed: {
            collapse () {
                return this.$store.state.common.collapse
            },
            routerList () {
                return this.$store.state.user.routerList
            },
            activePath () {
                return this.$route.path
            }
        },
        watch: {
            routerList: {
                deep: true,
                immediate: true,
                handler (newVal) {
                    if (newVal && newVal.length) {
                        this.menuList = this.treeByPid(JSON.parse(JSON.stringify(newVal)))
                    }
                }
            }
        },
        methods: {
            // 菜单列表转换成树形菜单结构
            treeByPid (arr) {
                const tree = []
                const map = new Map(arr.map(item => [item.id, item]))
                map.forEach(item => {
                    if (item.isMenu !== '1') { return }
                    if (!item.pid) {
                        tree.push(item)
                        return
                    }
                    const parent = map.get(item.pid)
                    if (!parent) { return }
                    if (!parent.children) {
                        parent.children = []
                    }
                    parent.children.push(item)
                })
                return tree
            },
            // 侧边栏折叠
            changeCollapse () {
                this.$store.commit('SET_COLLAPSE', !this.collapse)
            }
        }
    }
</script>

<style lang="scss">
.el-menu.el-menu--popup {
    background-color: #fff !important;
}
.system-aside {
    position: absolute;
    left: 0;
    top: 70px;
    bottom: 0;
    display: flex;
    flex-direction: column;
    transition: width .3s ease-in-out;
    width: 250px;
    padding-bottom: 70px;

    .aside-menu {
        overflow-y: scroll;
        flex: 1;

        .el-menu {
            border-right: 0;
        }

        .el-menu:not(.el-menu--collapse) {
            width: 250px;
        }

        &::-webkit-scrollbar {
            width: 0;
        }
    }

    .aside-bottom {
        position: absolute;
        bottom: 0;
        right: 0;
        width: 250px;
        height: 70px;
        line-height: 30px;
        padding: 15px 15px 0 15px;
        overflow: hidden;
        display: flex;
        justify-content: space-between;
        align-items: flex-start;
        box-shadow: -10px -1px 10px 0 rgba(0, 0, 0, .1);

        .bottom-text {
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
        }

        .collapse-btn {
            cursor: pointer;
            text-align: center;
            width: 30px;
            height: 30px;
            margin-left: 20px;
            flex-shrink: 0;
            transition: background-color .3s ease-in-out;
            border-radius: 50%;
            background-color: #c5c5c5;

            i {
                font-size: 18px;
            }

            &:hover {
                background: #e5e5e5;
            }
        }
    }

    &.is-collapse {
        width: 64px;
    }
}
</style>
